package edu.cmich.cps.cps240.partyprincesspalace.hw06;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.geom.Line2D;
import java.awt.image.BufferedImage;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JRadioButton;

/**
 * This button is used to start the wire creation process in the workspace
 * (Whatever that will end up being)
 * @author holde1ja
 */
public class WireButton extends JRadioButton {
	Workspace workspace;
	private static final long serialVersionUID = 1111593714065279058L;
	
	/**
	 * Initialises the icon for the wire button and sets our handle to
	 * the workspace where we will be creating wires to.
	 * @param p_workspace The workspace that this button will be telling
	 * to start adding wires.
	 */
	WireButton(Workspace p_workspace)
	{
		workspace = p_workspace;
		this.setIcon(generateWireIcon());
		this.addItemListener( new ItemListener() {

			@Override
			public void itemStateChanged(ItemEvent e) {
				System.out.println(((WireButton)e.getSource()).isSelected());
				workspace.setWireTool(((WireButton)e.getSource()).isSelected());
			}
			
		}
		);
	}
	
	/**
	 * @return A (very) rough icon for the wire button
	 */
	private static Icon generateWireIcon()
	{
		//Creates a buffered image, grabs its graphics context and draws a box
		//and a line in it.
		Graphics2D g2;
		BufferedImage img = new BufferedImage(40,30, BufferedImage.TYPE_4BYTE_ABGR);
		Rectangle rect = new Rectangle(0,-1, 40, 30);
		g2 = img.createGraphics();
		g2.setColor(Color.BLACK);
		g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
				RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);
		g2.draw(rect);
		g2.draw(new Line2D.Double(0, rect.getHeight(), rect.getWidth(), 0));
		ImageIcon icon = new ImageIcon(img);
		return icon;
		
	}
}
